rm(list=ls())
library(tidyverse)
library(scales)
library(broom)
library(ggpubr)
#df<-#import data24.csv as df


t<-list(
  lm(ftjudcourt~mem_crt+mem_pol+cne+pne+neg_aff+nat_ft_avg+female+educ+pidc+pid_dk+age+black+hispanic,data=df)%>%
    broom::tidy() %>%
  dplyr::filter(term %in% c("mem_crt","mem_pol")) %>%
  dplyr::select(term, estimate, std.error)%>%
    mutate(ci_low=estimate-(1.96*std.error),ci_up=estimate+(1.96*std.error),x=ifelse(term=="mem_pol", "p","c"),Evaluations="A. Court Evaluations",terms=ifelse(term=="mem_pol","Police experience", "Courts experience")),
                                                                                                                                                                       
  lm(ftpolice~mem_crt+mem_pol+cne+pne+neg_aff+nat_ft_avg+female+educ+pidc+pid_dk+age+black+hispanic,data=df)%>%
    broom::tidy() %>%
    dplyr::filter(term %in% c("mem_crt","mem_pol")) %>%
    dplyr::select(term, estimate, std.error)%>%
    mutate(ci_low=estimate-(1.96*std.error),ci_up=estimate+(1.96*std.error),x=ifelse(term=="mem_pol", "p","c"),Evaluations="B. Police Evaluations",terms=ifelse(term=="mem_pol","Police experience", "Courts experience")))%>%
  bind_rows(.[[1]], .[[2]])%>%
  ggplot(.,aes(x=estimate,y=terms,xmin=ci_low,xmax=ci_up,shape=terms,group=terms,color=terms))+
  geom_point(size=3)+
  geom_errorbar(aes(linetype=terms),width=.14)+
  xlab("")+
  scale_color_manual(values=c("black","black","black","black"))+
  scale_shape_manual(values=c(1,0))+
  scale_y_discrete(labels=c("  Court most memorable  ","  Police most memorable  ")) +
  #scale_linetype_manual(values=c(2,2,1,1))+
  scale_x_continuous(breaks = c(-12,-10,-8,-6,-4,-2,0,2,4,6,8,10,12),minor_breaks = 0,limits=c(-12,12))+
  geom_vline(xintercept = 0, linetype = 5,col="grey20")+
  #geom_hline(yintercept = 1.5, linetype = 2)+
  guides(color=guide_legend(title="Experiences"),shape=guide_legend(title="Experiences"),linetype=guide_legend("Experiences"))+
  ylab("")+
  facet_wrap(vars(Evaluations))+
  scale_linetype_manual(values =c("solid","longdash"))+
  #xlim(-.5,.12)+
  theme_bw()+
  theme(legend.position="none")


sv <- c("PVN","PSN","PN","PSP","PVP","CVN","CSN","CN","CSP","CVP")

b<-list(
  lm(ftjudcourt~PVN+PSN+PN+PSP+PVP+CVN+CSN+CN+CSP+CVP+neg_aff+nat_ft_avg+female+educ+pidc+pid_dk+age+black+hispanic,data=df)%>%
    broom::tidy() %>%
    dplyr::filter(term %in% sv) %>%
    dplyr::select(term, estimate, std.error)%>%
    mutate(ci_low=estimate-(1.96*std.error),ci_up=estimate+(1.96*std.error),Evaluations="C. Court Evaluations"),
  
  lm(ftpolice~PVN+PSN+PN+PSP+PVP+CVN+CSN+CN+CSP+CVP+neg_aff+nat_ft_avg+female+educ+pidc+pid_dk+age+black+hispanic,data=df)%>%
    broom::tidy() %>%
    dplyr::filter(term %in% sv) %>%
    dplyr::select(term, estimate, std.error)%>%
    mutate(ci_low=estimate-(1.96*std.error),ci_up=estimate+(1.96*std.error),Evaluations="D. Police Evaluations"))%>%
  bind_rows(.[[1]], .[[2]])%>%
  mutate(terms=rep(c("p","c"), each = 5, times = 4))%>%
  ggplot(.,aes(x=estimate,y=factor(term,levels=c("CVN","CSN","CN","CSP","CVP","PVN","PSN","PN","PSP","PVP")),xmin=ci_low,xmax=ci_up,shape=terms,group=term,color=term))+
  geom_point(size=3)+
  geom_errorbar(aes(linetype=terms),width=.14)+
  scale_shape_manual(values=c(1,0))+
  scale_x_continuous(breaks = c(-36,-30,-24,-18,-12,-6,0,6,12,18,24,30,36),minor_breaks = 0,limits=c(-36,36))+
  xlab("")+
  ylab("")+
  facet_wrap(vars(Evaluations))+
  scale_color_manual(values=c("black","black","black","black","black","black","black","black","black","black"))+
  scale_linetype_manual(values =c("solid","longdash","solid","longdash","solid","longdash","solid","longdash","solid","longdash"))+
  geom_vline(xintercept = 0, linetype = 5,col="grey20")+
  scale_y_discrete(labels=c("Court very negative",
                            "Court somewhat negative",
                            "Court neutral",
                            "Court somewhat positive",
                            "Court very positive",
                            "Police very negative",
                            "Police somewhat negative",
                            "Police neutral",
                            "Police somewhat positive",
                            "Police very positive"))+
  theme_bw()+
  theme(legend.position =  "none")


ggarrange(t,b,ncol=1,nrow=2,common.legend = FALSE,legend="none")









